package com.knowvoyage.knowvoyageserver.controller;

import com.github.pagehelper.PageInfo;
import com.knowvoyage.knowvoyageserver.pojo.entity.Message;
import com.knowvoyage.knowvoyageserver.pojo.vo.Result;
import com.knowvoyage.knowvoyageserver.service.MessageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "消息管理", description = "消息的增删改查接口")
@RestController
@RequestMapping("/message")
@RequiredArgsConstructor
public class MessageController {

    private final MessageService messageService;

    @Operation(summary = "创建消息", description = "创建新消息接口")
    @PostMapping("/insert")
    public Result<Boolean> insert(@RequestBody Message message) {
        return messageService.insert(message);
    }

    @Operation(summary = "删除消息", description = "根据ID删除消息接口")
    @GetMapping("/deleteById")
    public Result<Boolean> deleteById(
            @Parameter(description = "消息ID", required = true)
            @RequestParam Long messageId) {
        return messageService.deleteById(messageId);
    }

    @Operation(summary = "更新消息", description = "更新消息内容接口")
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody Message message) {
        return messageService.update(message);
    }

    @Operation(summary = "查询消息", description = "根据ID查询消息详情接口")
    @GetMapping("/select")
    public Result<Message> select(
            @Parameter(description = "消息ID", required = true)
            @RequestParam Long messageId) {
        return messageService.select(messageId);
    }

    @Operation(summary = "查询对话消息", description = "根据对话ID查询消息列表接口")
    @GetMapping("/selectByDialogId")
    public Result<List<Message>> selectByDialogId(
            @Parameter(description = "对话ID", required = true)
            @RequestParam Long dialogId) {
        return messageService.selectByDialogId(dialogId);
    }

    @Operation(summary = "查询最后一条消息", description = "获取对话的最后一条消息接口")
    @GetMapping("/selectLastByDialogId")
    public Result<Message> selectLastByDialogId(
            @Parameter(description = "对话ID", required = true)
            @RequestParam Long dialogId) {
        return messageService.selectLastByDialogId(dialogId);
    }

    @Operation(summary = "统计消息数量", description = "统计对话中的消息数量接口")
    @GetMapping("/countByDialogId")
    public Result<Integer> countByDialogId(
            @Parameter(description = "对话ID", required = true)
            @RequestParam Long dialogId) {
        return messageService.countByDialogId(dialogId);
    }

    @Operation(summary = "分页查询对话消息", description = "根据对话ID分页查询消息")
    @GetMapping("/selectByDialogIdPage")
    public Result<PageInfo<Message>> selectByDialogIdPage(
            @Parameter(description = "对话ID", required = true)
            @RequestParam Long dialogId,
            @Parameter(description = "页码", example = "1")
            @RequestParam(defaultValue = "1") int page,
            @Parameter(description = "每页数量", example = "10")
            @RequestParam(defaultValue = "10") int size) {
        return messageService.selectByDialogIdPage(dialogId, page, size);
    }
}